import {
  indexLottery,lotteryList
} from '../../api/index.js'
const handleAuthorize = require('../../utils/handleAuthorize')
Component({
  data: {
    deg: 0,
    singleAngle: '',
    // 每片扇形的角度
    isStart: false,
    isToken: false, //是否登录
    recordLs:[],//中奖记录数组
  },
  properties: {
    // 划分区域
    areaNumber: {
      type: Number,
      value: 6,
    },
    // 速度
    speed: {
      type: Number,
      value: 16,
    },
  },
  lifetimes: {
    created: function () {
      // 在组件实例刚刚被创建时执行
      // console.log('子组件————————created')
    },
    attached: function () {
      // 在组件实例进入页面节点树时执行
      this.init()
      this.setData({
        isToken: handleAuthorize.validate_login()
      })
      // console.log('子组件————————attached')
    },
    ready: function () {
      // 在组件在视图层布局完成后执行
      // console.log('子组件————————ready')
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
      // console.log('子组件————————detached')
    },
  },
  methods: {
    // 取消登录
    cancelFunc() {},
    // 确认登录
    confirmFunc() {
      handleAuthorize.jumpPublicLogin()
    },
    //抽奖记录弹窗
    recordView(){
      this.getLotteryList()
    },
    //关闭弹窗
    closeRecordView(){
      const childComponent = this.selectComponent('#recordPopup');
      childComponent.close();
    },
    //获取抽奖记录
    async getLotteryList(){
      let res = await lotteryList()
      this.setData({
        recordLs: res.result
      })
      const childComponent = this.selectComponent('#recordPopup');
      childComponent.open();
    },
    //初始化
    init() {
      const singleAngle = 360 / this.properties.areaNumber;
      this.data.singleAngle = singleAngle;
    },

    // 点击开始抽奖
    start() {
      // this.data.$emit('start');
      if (this.data.isToken) {
        // this.begin(0)
        // return
        handleAuthorize.preventActive(() => {
          this.getIndexLottery()
        })
      } else {
        const childComponent = this.selectComponent('#loginPopupId');
        childComponent.openPopup();
      }
    },
    //获取获奖奖品
    async getIndexLottery() {
      let res = await indexLottery()
      this.begin(res.result.prize)
    },

    begin(awardNumer) {
      let integral = awardNumer
      var deg = this.data.deg;
      var singleAngle = this.data.singleAngle;
      var speed = this.properties.speed;
      var isStart = this.data.isStart;
      if (isStart) return;
      this.data.isStart = true;
      let endAddAngle = 0;
      endAddAngle = 360 - ((awardNumer - 1) * singleAngle + singleAngle / 2); // 中奖角度
      const rangeAngle = (Math.floor(Math.random() * 4) + 4) * 360; // 随机旋转几圈再停止
      let cAngle;
      deg = 0;
      this.data.timer = setInterval(() => {
        if (deg < rangeAngle) {
          deg += speed;
        } else {
          cAngle = (endAddAngle + rangeAngle - deg) / speed;
          cAngle = cAngle > speed ? speed : cAngle < 1 ? 1 : cAngle;
          deg += cAngle;

          if (deg >= endAddAngle + rangeAngle) {
            deg = endAddAngle + rangeAngle;
            this.data.isStart = false;
            clearInterval(this.data.timer);
            // this.data.$emit('success');
            if(integral==0){
              tt.showToast({
                title: '很遗憾，没有获得积分~',
                icon:'none',
              });
            }else{
              tt.showToast({
                icon:'none',
                title: '获得' + integral + '积分，请联系客服',
              });
            }
          }
        }
        this.data.deg = deg;
        this.setData({
          deg: deg
        })
      }, 1000 / 60);
    }
  }
})